package gen.tools.delivery;

import gen.tools.Widgets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryDevliver extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QueryDevliver() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		Connection tarConn = null;
		
		int targetSystemId =	Integer.parseInt(request.getParameter("targetSystemId"));	//提交對象的系統的身份
		
		String deliverSN_q = "".equalsIgnoreCase(request.getParameter("deliverSN_q"))?"":"t1.deliverySN="+ request.getParameter("deliverSN_q")+" AND ";  
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		final ArrayList<Struc> strucs = new ArrayList<Struc>();
		final boolean isSU = Boolean.parseBoolean( request.getParameter("isSU") );
		final String currentUser = request.getParameter("empId");
		String subAccess = " t2.subsysid IN(";
		
		String sql = null;
		
		
		try {
			conn = wid.getConn();
			tarConn = wid.getTarConnection(targetSystemId);		//取得接收方的資料庫連結
			
			if ( isSU ) {
				subAccess = "";
			} else {
				sql = "SELECT subsysid FROM mpemprolesubsys WHERE empId=? AND roleid=7";
				ps = conn.prepareStatement(sql);
				ps.setString(1, currentUser );
				rs = ps.executeQuery();
				while ( rs.next() ) {
					subAccess += "'"+rs.getString("subsysid")+"',";
				}
				subAccess = subAccess.substring(0, subAccess.length()-1);
				subAccess += ") AND ";
			}
			sql = "SELECT t6.subsysDesc,t2.subsysid,t1.deliverySN,t1.deliveryComment,t1.deliveryTarSN,t1.deplmainSN,t5.empName,t2.creator,t2.createdDateTime,t4.statusSN,t4.statusdesc FROM deltask t1,deplmain t2,envsystemlist t3,defdeltask t4,emps t5,subsyses t6 WHERE "+deliverSN_q +"t2.creator=t5.empId AND t6.subsysid=t2.subsysid AND t1.isDeliver='t' AND t1.deplmainSN=t2.deplmainSN AND "+subAccess+" t3.sysSN=t1.targetSystemId AND t3.islocalsys='f' AND t1.statusSN=t4.statusSN ORDER BY deliverySN DESC";
			ps = tarConn.prepareStatement(sql);
			rs = ps.executeQuery();
			while ( rs.next() ) {
				final Struc sts = new Struc();
				sts.setSubsysId( rs.getString("subsysid") );
				sts.setSubsysDesc( rs.getString("subsysDesc") );
				sts.setComment( rs.getString("deliveryComment") );
				sts.setDeliveryTarSN(rs.getInt("deliveryTarSN"));
				sts.setCreator( rs.getString("creator") );
				sts.setCreatedDateTIme(wid.changeTimeFormat(rs.getTimestamp("createdDateTime")));
				sts.setDeplmainSN( rs.getInt("deplmainSN") );
				sts.setStatusdesc( rs.getString("statusdesc") );
				sts.setDeliverySN( rs.getInt("deliverySN") );
				sts.setCreatorName( rs.getString("empName") );
				sts.setStatusSN( rs.getInt("statusSN") );
				strucs.add(sts);
			}
		} catch ( Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
				if (tarConn!= null) {tarConn.close();}
			} catch (SQLException e) { System.out.println(wid.errProcess(e)); }
		}
		
		response.getWriter().write(new Gson().toJson(strucs));
	
		
	}

}
